package com.jesterplayer.fragment;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.actionbarsherlock.app.SherlockFragment;
import com.jesterplayer.R;
import com.jesterplayer.adapter.GenreAdapter;

/*public class GenreFragment extends SherlockFragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_library, container,
				false);
		TextView text = new TextView(getActivity());
		text.setText("I'm sexy and I know it song");
		
		return text;
	}*/
	
public class GenreFragment extends SherlockFragment implements
		LoaderCallbacks<Cursor>, OnItemClickListener {

	// Adapter
    private GenreAdapter mGenreAdapter;
	
	// ListView
	private ListView mListView;

	// Cursor
	private Cursor mCursor;

	// Current genre Id
	private String mCurrentGenreId;

	// Options
	private final int PLAY_SELECTION = 14;

	// Audio columns
	public static int mGenreIdIndex, mGenreNameIndex;

	// Bundle
	public GenreFragment() {
	}

	public GenreFragment(Bundle args) {
		setArguments(args);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		// GenreAdapter
		mGenreAdapter = new GenreAdapter(getActivity(),
				R.layout.listview_items, null, new String[] {}, new int[] {}, 0);
		mListView.setOnCreateContextMenuListener(this);
		mListView.setOnItemClickListener(this);
		mListView.setAdapter(mGenreAdapter);

		// Important!
		getLoaderManager().initLoader(0, null, this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.listview, container, false);
		mListView = (ListView) root.findViewById(android.R.id.list);
		return root;
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		String[] projection = new String[] { Audio.Genres._ID,
				Audio.Genres.NAME };
		Uri uri = Audio.Genres.EXTERNAL_CONTENT_URI;
		String sortOrder = Audio.Genres.DEFAULT_SORT_ORDER;
		return new CursorLoader(getActivity(), uri, projection, null, null,
				sortOrder);
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// Check for database errors
		if (data == null) {
			return;
		}

		mGenreIdIndex = data.getColumnIndexOrThrow(Audio.Genres._ID);
		mGenreNameIndex = data.getColumnIndexOrThrow(Audio.Genres.NAME);
		mGenreAdapter.changeCursor(data);
		mCursor = data;
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		if (mGenreAdapter != null)
			mGenreAdapter.changeCursor(null);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putAll(getArguments() != null ? getArguments() : new Bundle());
		super.onSaveInstanceState(outState);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		
	}
}
